package com.qububu.share.article.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.share.article.dto.ArticleInfoDto;
import com.qububu.share.article.service.ArticleService;
import com.qububu.utils.DateUtil;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

/**
 * @功能 文章的相关处理
 * @详情 
 * */
@Controller
@RequestMapping("/article")
public class ArticleControl {

	@Resource
	private ArticleService articleService;
	
	/**
	 * @功能 添加文章部分和全部内容
	 * @author LH
	 * */
	@RequestMapping("/addArticle")
	public String addArticle(HttpServletRequest request, HttpServletResponse response) {
		ArticleInfoDto articleInfo = gainArticleInfoDto(request, response);
		
		articleService.doAddArticle(articleInfo);
		
		return "index"; 
	}
	
	@RequestMapping("/test")
	public String test() {
		
		return "test"; 
	}
	
	/**
	 * @功能   查询文章列表
	 * @author LH
	 * */
	@RequestMapping("/queryArticle")
	public String queryArticle(HttpServletRequest request, HttpServletResponse response) {
		ArrayList<ArticleInfoDto> articleList = articleService.getQueryArticle();
		
		request.setAttribute("list", articleList);
		
		return "testArticleList";
	}
	
	/**
	 * @功能   查询文章详情内容
	 * @author LH
	 * */
	@RequestMapping("/queryDetailArticle")
	public String queryDetailArticle(HttpServletRequest request, HttpServletResponse response) {
		String article_id = assertNotEmpty(request.getParameter("article_id"));
		
		ArticleInfoDto articleInfo = articleService.getQueryDetailArticle(article_id);
		request.setAttribute("articleInfo", articleInfo);
		
		return "testArticleList";
	}
	
	/**
	 * @功能 删除文章
	 * @author LH
	 * */
	@RequestMapping(value="/ajaxDeleteArticle")
	@ResponseBody
	public boolean ajaxDeleteArticle(HttpServletRequest request, HttpServletResponse response) {
		String articleId = assertNotEmpty(request.getParameter("article_id"));
		
		articleService.doDeleteArticle(articleId);
		
		return true;
	} 
	
	/**
	 * @功能 得到文章内容
	 * @author LH
	 * @return ArticleInfoDto 文章的内容
	 */
	private ArticleInfoDto gainArticleInfoDto(HttpServletRequest request, HttpServletResponse response){
		String articleTitle = SystemUtil.convertSpecialMark(assertNotEmpty(request.getParameter("article_title")));
		String articleContent = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(request.getParameter("article_text")));
		String articleType = assertNotEmpty(request.getParameter("article_type"));
		
		int summaryContentLength = 80;
		UserInfoDto dto = (UserInfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String userId = String.valueOf(dto.getUserId());
		String artSummaryPicUrl = SystemUtil.getSummaryPicUrl(articleContent, request.getContextPath());
		String articleSummaryText = SystemUtil.getSummaryText(articleContent, summaryContentLength);
		String articleCreateIp = assertNotEmpty(SystemUtil.getRequestIp(request));
		int articleTypeInt = ParamConverter.convertToInteger(articleType);
		int articleCreateDttm = DateUtil.convertCurrentDTTMtoInt();
		String articleId = SystemUtil.getUUIDString();
		
		return createArticleInfoDto(articleId, articleTitle, SystemUtil.convertSpecialMark(articleContent), articleSummaryText, artSummaryPicUrl, articleCreateDttm, articleCreateIp, articleTypeInt, userId);
	}
	
	/**
	 * @功能 设置文章的内容
	 * @author LH
	 * @return ArticleInfoDto 设置过后的文章内容
	 */
	private ArticleInfoDto createArticleInfoDto(String articleId, String articleTitle, String articleContent, 
			String articleSummaryText, String artSummaryPicUrl, int articleCreateDttm, 
			String articleCreateIp, int articleTypeInt, String userId){
		ArticleInfoDto articleInfo = new ArticleInfoDto(articleId, userId);
		
		articleInfo.setArticleTitle(articleTitle);
		articleInfo.setArticleText(articleContent);
		articleInfo.setArticleSummaryText(articleSummaryText);
		articleInfo.setArtSummaryPicUrl(artSummaryPicUrl);
		articleInfo.setArticleCreateDttm(articleCreateDttm);
		articleInfo.setArticleCreateIp(articleCreateIp);
		articleInfo.setArticleType(articleTypeInt);
		
		return articleInfo;
	}
}
